package org.darcy.service.privilege.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.darcy.dao.privilege.SysRoleMapper;
import org.darcy.entity.privilege.SysRole;
import org.darcy.entity.privilege.SysRoleMenu;
import org.darcy.entity.privilege.SysUser;
import org.darcy.entity.privilege.SysUserRole;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.service.privilege.SysRoleMenuService;
import org.darcy.service.privilege.SysRoleService;
import org.darcy.service.privilege.SysUserRoleService;
import org.darcy.service.privilege.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Service
public class SysRoleServiceImpl extends GenericServiceImpl<SysRole> implements SysRoleService {
	@Lazy
	@Autowired
	private SysUserRoleService SysUserRoleService;

	@Autowired
	private SysUserService userService;

	@Lazy
	@Autowired
	private SysRoleMenuService SysRoleMenuService;

	@Autowired
	private SysRoleMapper SysRoleMapper;

	@Override
	public PageData<SysRole> page(PageParam<SysRole> param) {
		// 参数一是当前页，参数二是每页个数
		SysRole role = param.getEntity();
		LambdaQueryWrapper<SysRole> groupLambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(role.getName())) {
			groupLambdaQueryWrapper.like(SysRole::getName, role.getName()).or().like(SysRole::getSn, role.getName());
		}
		IPage<SysRole> queryPage = new Page<>(param.getQuery().getPageNum(), param.getQuery().getPageSize());
		IPage<SysRole> page = this.getPage(queryPage, groupLambdaQueryWrapper);
		if (CollectionUtils.isNotEmpty(page.getRecords())) {
			page.getRecords().forEach(g -> {
				LambdaQueryWrapper<SysUserRole> userGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
				userGroupLambdaQueryWrapper.eq(SysUserRole::getRoleId, g.getId());
				List<SysUserRole> userGroups = SysUserRoleService.getList(userGroupLambdaQueryWrapper);
				if (CollectionUtils.isNotEmpty(userGroups)) {
					List<Integer> userIds = new ArrayList<>();
					userGroups.forEach(userGroup -> userIds.add(userGroup.getUserId()));
					List<SysUser> users = userService.getByIds(userIds);
					g.setUsers(users);
				}
			});
		}
		PageData<SysRole> pageData = new PageData<SysRole>(page, param);
		return pageData;
	}

	@Override
	public List<SysRole> getListByUserId(Integer userId) {
		LambdaQueryWrapper<SysUserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
		userRoleLambdaQueryWrapper.eq(SysUserRole::getUserId, userId);
		List<SysUserRole> userGroups = SysUserRoleService.getList(userRoleLambdaQueryWrapper);
		if (CollectionUtils.isNotEmpty(userGroups)) {
			List<Integer> groupIds = new ArrayList<>();
			userGroups.forEach(ur -> groupIds.add(ur.getRoleId()));
			LambdaQueryWrapper<SysRole> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
			roleLambdaQueryWrapper.in(SysRole::getId, groupIds);
			List<SysRole> roles = this.getList(roleLambdaQueryWrapper);
			return roles;
		}
		return null;
	}

	@Override
	public List<SysRole> getAll(SysRole group) {
		LambdaQueryWrapper<SysRole> groupLambdaQueryWrapper = new LambdaQueryWrapper<>();
		List<SysRole> userGroups = SysRoleMapper.selectList(groupLambdaQueryWrapper);
		return userGroups;
	}

	@Override
	public void deleteByIds(List<String> groupIds) {
		if (CollectionUtils.isNotEmpty(groupIds)) {
			// 1、删除权限值
			LambdaQueryWrapper<SysRoleMenu> aclLambdaQueryWrapper = new LambdaQueryWrapper<>();
			aclLambdaQueryWrapper.in(SysRoleMenu::getRoleId, groupIds);
			SysRoleMenuService.deleteBatch(aclLambdaQueryWrapper);
			// 2、删除用户组关联
			LambdaQueryWrapper<SysUserRole> userGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
			userGroupLambdaQueryWrapper.eq(SysUserRole::getRoleId, groupIds);
			SysUserRoleService.deleteBatch(userGroupLambdaQueryWrapper);
			// 3、删除组
			this.deleteByIds(groupIds);
		}
	}
}
